using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_PlaceManagement : admin_BaseUserControl
{
    PlaceDAO daoPlace = new PlaceDAO();
    Place place = new Place();
    #region properties
    public string ImageURL
    {
        get { return place.ImageURL; }
    }

    #endregion
    #region methods

    /// <summary>
    /// Load data to page
    /// </summary>    
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "place";
        LoadData();         
        if (Request["mode"] == "1")
        {
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            try
            {
                int id = int.Parse(Request["id"]);
                CurrentID = Request["id"];
                place = daoPlace.GetPlace(id);
                txtName.Text = place.Name;
                txtTag.Text = place.Tag;
                txtDescription.Text = place.Description;
                chkIsRecommended.Checked = place.IsRecommended;
                txtRank.Text = place.Rank.ToString();
                chkState.Checked = place.State;
                fckArticle.Value = place.Article;
                //set category in dropdown
                int index = 0;
                foreach (ListItem item in cmbType.Items)
                {
                    if (item.Value == place.Category.Id.ToString())
                    {
                        cmbType.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
                //set location in dropdown
                index = 0;
                foreach (ListItem item in cmbLocation.Items)
                {
                    if (item.Value == place.Location.Id.ToString())
                    {
                        cmbLocation.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
                
            }
            catch (Exception ex)
            {
                ShowMessage("Error occured when trying to load place information.");
            }
        }
        else
        {          
                lnkAdd.Visible = true;
                lnkUpdate.Visible = false;
                lnkDelete.Visible = false;
           
        }
    }
    /// <summary>
    /// Load initialize data
    /// </summary>
    public void LoadData()
    {
        PlaceCategoryDAO daoPlaceCate = new PlaceCategoryDAO();
        LocationDAO daoLocation = new LocationDAO();
        cmbType.DataSource = daoPlaceCate.GetAllPlaceCategories();
        cmbType.DataTextField = "Name";
        cmbType.DataValueField = "Id";
        cmbLocation.DataSource = daoLocation.GetAllLocations();
        cmbLocation.DataTextField = "Name";
        cmbLocation.DataValueField = "Id";
        cmbType.DataBind();
        cmbLocation.DataBind();
        
    }

 
    /// <summary>
    /// Implementation for update event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        int id = int.Parse(CurrentID);
        place.Name = txtName.Text;
        place.Description = txtDescription.Text;
        place.Article = fckArticle.Value;
        place.ImageURL=Request["txtImage"];
        place.Location.Id = int.Parse(cmbLocation.SelectedValue);
        place.Category.Id = int.Parse(cmbType.SelectedValue);
        place.Rank = int.Parse(txtRank.Text);
        place.IsRecommended = chkIsRecommended.Checked;
        place.State = chkState.Checked;
        place.ModifiedDate = DateTime.Now;
        place.ModifiedBy = 1;
        if (daoPlace.Update(place) > 0)
        {
            ShowMessage("The information is updated");
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
        }
        else ShowMessage("Error occured when trying to update information about the place. Action failed");
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int id = int.Parse(CurrentID);
        if (daoPlace.Delete(id) > 0)
        {
            ShowMessage("The information is deleted");
            txtName.Text = "";
            txtDescription.Text = "";
            txtTag.Text = "";
            chkIsRecommended.Checked = false;
            chkState.Checked = false;
            fckArticle.Value = "";
            lnkAdd.Visible = true;
            lnkDelete.Visible = false;
            lnkUpdate.Visible = false;
            place.ImageURL = "";
        }
        else ShowMessage("Error occured when trying to delete information about the place. Action failed");
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        place.Name = txtName.Text;
        place.Description = txtDescription.Text;
        place.Article = fckArticle.Value;
        place.ImageURL = Request["txtImage"];
        place.Location.Id = int.Parse(cmbLocation.SelectedValue);
        place.Category.Id = int.Parse(cmbType.SelectedValue);
        place.IsRecommended = chkIsRecommended.Checked;
        place.State = chkState.Checked;
        place.ModifiedDate = DateTime.Now;
        place.Rank = int.Parse(txtRank.Text);
        place.ModifiedBy = 1;
        if (daoPlace.Insert(place) > 0)
        {
            ShowMessage("New place is created");
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            CurrentID = place.Id.ToString();
        }
        else ShowMessage("Error occured when trying to create new place. Action failed");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lnkAdd_Click(sender, e);
    }
    #endregion
}
